package com.tiandao.mofang.ui;

import com.tiandao.mofang.core.TuoGuan;
import com.tiandao.mofang.core.bpjn.DianSkills;
import com.tiandao.mofang.core.jingji.LuJian;
import com.tiandao.mofang.core.richang.*;
import com.tiandao.mofang.core.shicai.ZhuDi;
import com.tiandao.mofang.core.shilian.BingQiPu;
import com.tiandao.mofang.core.shimei.PaiKe;
import com.tiandao.mofang.core.util.OpenBox;
import com.tiandao.mofang.core.util.YouJian;
import org.springframework.util.ClassUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.util.Date;

public class MFFrame extends JFrame {
    //托盘图标实例
    private static TrayIcon trayIcon = null;
    //获得系统托盘实例
    private static SystemTray systemTray = SystemTray.getSystemTray();
    //图标
    private static Image image;
    //图标菜单
    private static JPopupMenu popupMenu = new JPopupMenu();
    public static Thread t = null;
    public static int tKey = 1;
    //面板(容器)
    private static JPanel panel = new JPanel();
    private static JMenuItem exit = new JMenuItem("  退 出 程 序");
    private static JButton zdStartButton = new JButton("开始捡食材(驻地)!");
    private static JButton zdStopButton = new JButton("停止捡食材(驻地)!");
    private static JButton jjLJStartButton = new JButton("论剑周常开始!");
    private static JButton jjLJStopButton = new JButton("论剑周常结束!");
    private static JButton dzStartButton = new JButton("打坐开始!");
    private static JButton dzStopButton = new JButton("打坐结束!");
    //日常按钮
    private static JButton rcStartButton = new JButton("日常开始!");
    private static JButton rcStopButton = new JButton("日常停止!");
    //
    private static JButton jyTenStartButton = new JButton("剑影十开!");
    private static JButton jyTenStopButton = new JButton("剑影十关!");

    private static JButton bqpStartButton = new JButton("兵器谱开!");
    private static JButton bqpStopButton = new JButton("兵器谱关!");

    private static JButton tgStartButton = new JButton("托管游戏!");
    private static JButton tgStopButton = new JButton("停止托管");

    //点技能按钮
    private static JButton buttonOpenSkills = new JButton("点技能开始");
    private static JButton buttonStopSkills = new JButton("点技能停止");

    //开箱按钮
    private static JButton buttonOpenBox = new JButton("开箱");
    private static JButton buttonStopBox = new JButton("停止");

    //开箱按钮
    private static JButton yjStartButton = new JButton("收邮件开始");
    private static JButton yjStopButton = new JButton("收邮件结束");

    //师妹排课
    private static JButton paiKeStartButton = new JButton("师妹排课开始");
    private static JButton paiKeStopButton = new JButton("师妹排课结束");
    //号数单选框
    public static ButtonGroup bgHao = new ButtonGroup();
    public static JRadioButton h1 = new JRadioButton("一号",true);
    public static JRadioButton h2 = new JRadioButton("二号");
    public static JRadioButton h3 = new JRadioButton("三号");
    public static JRadioButton h4 = new JRadioButton("四号");
    public static JRadioButton h5 = new JRadioButton("五号");

    //开箱按钮
    private static JButton HaoShuButton = new JButton("确定号数");

    //日常单选框
    public static ButtonGroup bgRiChang = new ButtonGroup();
    public static JRadioButton r1 = new JRadioButton("杭州东郊",true);
    public static JRadioButton r2 = new JRadioButton("东越香蝶");
    public static JRadioButton r3 = new JRadioButton("东越万蝶");
    public static JRadioButton r4 = new JRadioButton("九华华清");
    public static JRadioButton r5 = new JRadioButton("九华华清");
    public static JRadioButton r6 = new JRadioButton("九华华清");
    public static JRadioButton r7 = new JRadioButton("九华华清");
    public static JRadioButton r8 = new JRadioButton("九华华清");
    public static JRadioButton r9 = new JRadioButton("九华华清");
    public static JRadioButton r10 = new JRadioButton("九华华清");

    //日志
    public static JTextArea textArea = new JTextArea();
    //清除日志按钮
    private static JButton buttonClear = new JButton("清除日志");


    //静态代码块 类加载时加载
    static {
        //容器设置
        panel.setLayout(null);
        panel.setBounds(0,0,1150,630);
        panel.setBackground(new Color(240, 240, 240));
        //设置图标
//        System.out.println(ScFrame.class.getClassLoader().getResource("static/icon.png").getPath());
        try {
            //文件系统获取方法
//            image = ImageIO.read(new File(ScFrame.class.getClassLoader().getResource("static/icon.png").getPath()));
            System.out.println(MFFrame.class.getClassLoader().getResource("static/icon.png").getPath());
            //打成jar包获取方法输入流获取
            image = ImageIO.read(ClassUtils.getDefaultClassLoader().getResourceAsStream("static/icon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //给图标菜单exit添加退出程序事件
        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        popupMenu.add(exit);
        //设置图标
        trayIcon = new TrayIcon(image, "收集食材");
        //托盘图标添加监听事件(鼠标点击)
        trayIcon.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                maybeShowPopup(e);
            }

            @Override
            public void mousePressed(MouseEvent e) {
                maybeShowPopup(e);
            }
            //获取鼠标点击坐标展示菜单
            private void maybeShowPopup(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popupMenu.setLocation(e.getX()-90, e.getY()-45);
                    popupMenu.setInvoker(popupMenu);
                    popupMenu.setVisible(true);
                }
            }
        });
        //驻地食材
        zdStartButton.setBackground(new Color(255,255,255));
        zdStartButton.setBounds(30,20,160,40);
        zdStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println(t);
                if(tKey == 1){
                    tKey++;
                    ZhuDi zhuDi = new ZhuDi();
                    t = new Thread(zhuDi);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
            }
        });
        zdStopButton.setBackground(new Color(255,255,255));
        zdStopButton.setBounds(30,70,160,40);
        zdStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":    停止捡食材!\n");
            }
        });


        //论剑周常
        jjLJStartButton.setBackground(new Color(255,255,255));
        jjLJStartButton.setBounds(210,20,130,40);
        jjLJStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              if(tKey == 1){
                    tKey++;
                    LuJian luJian = new LuJian();
                    t = new Thread(luJian);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    论剑周常开始!\n");
            }
        });

        jjLJStopButton.setBackground(new Color(255,255,255));
        jjLJStopButton.setBounds(210,70,130,40);
        jjLJStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              if(t!=null){
                    t.stop();
              }
              tKey=1;
              textArea.append(new Date()+":    论剑周常结束!\n");
            }
        });





        //打坐
        dzStartButton.setBackground(new Color(255,255,255));
        dzStartButton.setBounds(360,20,130,40);
        dzStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    DaZuo daZuo = new DaZuo();
                    t = new Thread(daZuo);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    打坐开始!\n");
            }
        });
        dzStopButton.setBackground(new Color(255,255,255));
        dzStopButton.setBounds(360,70,130,40);
        dzStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":    打坐结束!\n");
            }
        });

        //兵器谱
        bqpStartButton.setBackground(new Color(255,255,255));
        bqpStartButton.setBounds(660,20,130,40);
        bqpStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    BingQiPu bingQiPu = new BingQiPu();
                    t = new Thread(bingQiPu);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    兵器谱开始!\n");
            }
        });
        bqpStopButton.setBackground(new Color(255,255,255));
        bqpStopButton.setBounds(660,70,130,40);
        bqpStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":    兵器谱结束!\n");
            }
        });


        //日常
        rcStartButton.setBackground(new Color(255,255,255));
        rcStartButton.setBounds(970,150,130,25);
        rcStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    if(r1.isSelected()){
                        Hzdjcxxz h = new Hzdjcxxz();
                        t = new Thread(h);
                        t.start();
                    }
                    if(r2.isSelected()){
                        DyXdNzjy dyXdNzjy = new DyXdNzjy();
                        t = new Thread(dyXdNzjy);
                        t.start();
                    }
                    if(r3.isSelected()){
                        DywdYylk dywdYylk = new DywdYylk();
                        t = new Thread(dywdYylk);
                        t.start();
                    }
                    if(r4.isSelected()){
                        Jhjhshzs jhjhshzs = new Jhjhshzs();
                        t = new Thread(jhjhshzs);
                        t.start();
                    }
                    if(r5.isSelected()){
                        Hzdjcxxz h = new Hzdjcxxz();
                        t = new Thread(h);
                        t.start();
                    }
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    日常开始!\n");
            }
        });
        rcStopButton.setBackground(new Color(255,255,255));
        rcStopButton.setBounds(970,180,130,25);
        rcStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":    日常结束!\n");
            }
        });



        //剑影十
        jyTenStartButton.setBackground(new Color(255,255,255));
        jyTenStartButton.setBounds(510,20,130,40);
        jyTenStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    DaZuo daZuo = new DaZuo();
                    t = new Thread(daZuo);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    打坐开始!\n");
            }
        });
        jyTenStopButton.setBackground(new Color(255,255,255));
        jyTenStopButton.setBounds(510,70,130,40);
        jyTenStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":    打坐结束!\n");
            }
        });

        //托管游戏
        tgStartButton.setBackground(new Color(255,255,255));
        tgStartButton.setBounds(510,320,130,40);
        tgStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    TuoGuan tuoGuan = new TuoGuan();
                    t = new Thread(tuoGuan);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    开始游戏托管!\n");
            }
        });
        tgStopButton.setBackground(new Color(255,255,255));
        tgStopButton.setBounds(510,370,130,40);
        tgStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":     结束游戏托管!\n");
            }
        });

        //邮件
        yjStartButton.setBackground(new Color(255,255,255));
        yjStartButton.setBounds(300,320,130,40);
        yjStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    YouJian youJian  = new YouJian();
                    t = new Thread(youJian);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    开始批量收邮件!\n");
            }
        });
        yjStopButton.setBackground(new Color(255,255,255));
        yjStopButton.setBounds(300,370,130,40);
        yjStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":     停止批量收邮件!!\n");
            }
        });

        //点技能开始按钮样式设置
        buttonOpenSkills.setBackground(new Color(255,255,255));
        buttonOpenSkills.setBounds(30,320,100,40);
        buttonOpenSkills.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1) {
                    tKey++;
                    DianSkills dianSkills = new DianSkills();
                    t = new Thread(dianSkills);
                    t.start();
                    textArea.append(new Date() + "   点技能中!\n");
                }else{
                    textArea.append(new Date() + "   线程已存在!\n");
                }
            }
        });
        //点技能停止按钮样式设置
        buttonStopSkills.setBackground(new Color(255,255,255));
        buttonStopSkills.setBounds(30,370,100,40);
        buttonStopSkills.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date() + "   点技能停止!\n");
            }
        });


        //开箱开始按钮样式设置
        buttonOpenBox.setBackground(new Color(255,255,255));
        buttonOpenBox.setBounds(180,320,60,40);
        buttonOpenBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1) {
                    tKey++;
                    OpenBox openBox = new OpenBox();
                    t = new Thread(openBox);
                    t.start();
                    textArea.append(new Date() + "   开箱中!\n");
                }else{
                    textArea.append(new Date() + "   线程已存在!\n");
                }
            }
        });
        buttonStopBox.setBackground(new Color(255,255,255));
        buttonStopBox.setBounds(180,370,60,40);
        buttonStopBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date() + "   开箱停止!\n");
            }
        });

        //师妹排课
        paiKeStartButton.setBackground(new Color(255,255,255));
        paiKeStartButton.setBounds(670,320,130,40);
        paiKeStartButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(tKey == 1){
                    tKey++;
                    PaiKe paiKe = new PaiKe();
                    t = new Thread(paiKe);
                    t.start();
                }else{
                    textArea.append(new Date()+":    线程已存在请不要重复开启!\n");
                }
                textArea.append(new Date()+":    开始师妹排课!\n");
            }
        });
        paiKeStopButton.setBackground(new Color(255,255,255));
        paiKeStopButton.setBounds(670,370,130,40);
        paiKeStopButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(t!=null){
                    t.stop();
                }
                tKey=1;
                textArea.append(new Date()+":     停止师妹排课!\n");
            }
        });

        buttonClear.setBackground(new Color(255,255,255));
        buttonClear.setBounds(1050,570,90,30);
        buttonClear.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setBounds(30,420,1090,150);
        jScrollPane.setViewportView(textArea);

        textArea.setBounds(30,420,1090,150);

        bgHao.add(h1);
        bgHao.add(h2);
        bgHao.add(h3);
        bgHao.add(h4);
        bgHao.add(h5);
        h1.setBounds(30,270,60,20);
        h2.setBounds(90,270,60,20);
        h3.setBounds(150,270,60,20);
        h4.setBounds(210,270,60,20);
        h5.setBounds(270,270,60,20);
        panel.add(h1);
        panel.add(h2);
        panel.add(h3);
        panel.add(h4);
        panel.add(h5);


        HaoShuButton.setBackground(new Color(255,255,255));
        HaoShuButton.setBounds(360,270,160,25);
        HaoShuButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(h1.isSelected()){
                    YouJian.HAO_NUM = 1;
                    DaZuo.HAO_NUM = 1;
                    LuJian.HAO_NUM = 1;
                    OpenBox.HAO_NUM = 1;
                    System.out.println("号数已选择:1");
                    textArea.append("号数已选择: 1\n");
                }
                if(h2.isSelected()){
                    YouJian.HAO_NUM = 2;
                    DaZuo.HAO_NUM = 2;
                    LuJian.HAO_NUM = 2;
                    OpenBox.HAO_NUM = 2;
                    System.out.println("号数已选择:2");
                    textArea.append("号数已选择: 2\n");
                }
                if(h3.isSelected()){
                    YouJian.HAO_NUM = 3;
                    DaZuo.HAO_NUM = 3;
                    LuJian.HAO_NUM = 3;
                    OpenBox.HAO_NUM = 3;
                    System.out.println("号数已选择:3");
                    textArea.append("号数已选择: 3\n");
                }
                if(h4.isSelected()){
                    YouJian.HAO_NUM = 4;
                    DaZuo.HAO_NUM = 4;
                    LuJian.HAO_NUM = 4;
                    OpenBox.HAO_NUM = 4;
                    System.out.println("号数已选择:4");
                    textArea.append("号数已选择: 4\n");
                }
                if(h5.isSelected()){
                    YouJian.HAO_NUM = 5;
                    DaZuo.HAO_NUM = 5;
                    LuJian.HAO_NUM = 5;
                    OpenBox.HAO_NUM = 5;
                    System.out.println("号数已选择:5");
                    textArea.append("号数已选择: 5\n");
                }

            }
        });

        bgRiChang.add(r1);
        bgRiChang.add(r2);
        bgRiChang.add(r3);
        bgRiChang.add(r4);
        bgRiChang.add(r5);
        bgRiChang.add(r6);
        bgRiChang.add(r7);
        bgRiChang.add(r8);
        bgRiChang.add(r9);
        bgRiChang.add(r10);
        r1.setBounds(30,125,100,20);
        r2.setBounds(130,125,100,20);
        r3.setBounds(230,125,100,20);
        r4.setBounds(330,125,100,20);
        r5.setBounds(430,125,100,20);
        r6.setBounds(30,155,100,20);
        r7.setBounds(130,155,100,20);
        r8.setBounds(230,155,100,20);
        r9.setBounds(330,155,100,20);
        r10.setBounds(430,155,100,20);
//        r1.setEnabled(false);
//        r2.setEnabled(false);
//        r3.setEnabled(false);
//        r4.setEnabled(false);
        r5.setEnabled(false);
        r6.setEnabled(false);
        r7.setEnabled(false);
        r8.setEnabled(false);
        r9.setEnabled(false);
        r10.setEnabled(false);
        panel.add(r1);
        panel.add(r2);
        panel.add(r3);
        panel.add(r4);
        panel.add(r5);
        panel.add(r6);
        panel.add(r7);
        panel.add(r8);
        panel.add(r9);
        panel.add(r10);
        panel.add(buttonClear);
        panel.add(jScrollPane);

        panel.add(zdStartButton);
        panel.add(zdStopButton);

        panel.add(jjLJStartButton);
        panel.add(jjLJStopButton);

        panel.add(dzStartButton);
        panel.add(dzStopButton);

        panel.add(rcStartButton);
        panel.add(rcStopButton);

        panel.add(jyTenStartButton);
        panel.add(jyTenStopButton);

        panel.add(bqpStartButton);
        panel.add(bqpStopButton);

        panel.add(tgStartButton);
        panel.add(tgStopButton);

        panel.add(buttonOpenSkills);
        panel.add(buttonStopSkills);

        panel.add(buttonOpenBox);
        panel.add(buttonStopBox);

        panel.add(yjStartButton);
        panel.add(yjStopButton);

        panel.add(paiKeStartButton);
        panel.add(paiKeStopButton);

        panel.add(HaoShuButton);

    }

    public MFFrame() throws HeadlessException {
        super();
        //设置布局:默认
        setLayout(null);
        //弹出的初始位置
        setLocation(385,225);
        //设置窗口大小
        setSize(1150,630);
        //设置标题
        setTitle("减负神器  (适用分辨率: 1920 * 1080)");
        //设置图标
        setIconImage(image);
        add(panel);
        //设置大小固定
        setResizable(false);
        //添加监听事件，窗口关闭：最小化到任务栏
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                setVisible(false);
                miniTray();
            }
        });
        //设置可见性
        setVisible(true);
    }

    private void miniTray() { // 窗口最小化到任务栏托盘
        trayIcon.setImageAutoSize(true);
        //图标添加点击事件
        trayIcon.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 1) {// 左键 1
                    systemTray.remove(trayIcon);
                    setVisible(true);
                    setExtendedState(Frame.NORMAL);
                    toFront();
                }
            }
        });
        try {
            systemTray.add(trayIcon);
        } catch (AWTException e1) {
            e1.printStackTrace();
        }
    }
}
